package public

import (
	"server/game/cache"
	"strings"

	"github.com/hwcer/cosgo/registry"
	"github.com/hwcer/yyds/context"
	"github.com/hwcer/yyds/errors"
)

func init() {
	Register(&lockItem{})
}

type lockItem struct {
}

func (this *lockItem) Caller(node *registry.Node, handle *context.Context) interface{} {
	f := node.Method().(func(*lockItem, *context.Context) interface{})
	return f(this, handle)
}

/**
 * @name lockItem
 * @param string key 物品唯一ID,用','分割。
 * 加锁物品
 */
func (this *item) LockItem(c *context.Context) interface{} {
	keys := c.GetString("key")
	if keys == "" {
		return errors.ErrArgEmpty
	}
	doc := cache.GetRole(c.Player.Updater)
	role := doc.All()
	data := map[string]int8{}
	for k, v := range role.LockItem {
		data[k] = v
	}

	itemColl := cache.GetItems(c.Player.Updater)
	arr := strings.Split(keys, ",")
	for _, v := range arr {
		if itemColl.Has(v) {
			data[v] = 1
		}
	}

	doc.Set("LockItem", data)

	return nil
}

/**
 * @name unlockItem
 * @param string key 物品唯一ID,用','分割。
 * 解锁物品
 */

func (this *item) UnlockItem(c *context.Context) interface{} {
	keys := c.GetString("key")
	if keys == "" {
		return errors.ErrArgEmpty
	}
	doc := cache.GetRole(c.Player.Updater)
	role := doc.All()
	data := map[string]int8{}
	for k, v := range role.LockItem {
		data[k] = v
	}

	arr := strings.Split(keys, ",")
	for _, v := range arr {
		delete(data, v)
	}

	doc.Set("LockItem", data)

	return nil
}
